package jp.co.toshiba.ffsa.action.csas;

import java.util.ArrayList;

import javax.annotation.Resource;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.dto.LoginDto;
import jp.co.toshiba.ffsa.form.csas.Csas202Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.csas.Csas202Service;

import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * 登録情報変更>入力内容確認
 * @author npc
 */
public class Csas202Action extends CommonAction {
    /** 画面ID */
    public String screenID = "CSAS202";
    @ActionForm
    @Resource
    protected Csas202Form csas202Form;

    @Resource
    protected Csas202Service csas202Service;

    /**
     * 初期処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = false)
    public String index() throws Exception {
        // ログイン済判定する
        if(this.judgeLogin(csas202Form.getSessionId())){
            try{
                // リファラ判定
                if(this.judgeRefferer()){
                    return "/csas/csas202.jsp";
                }else{
                    return this.topTransition;
                }
            }catch(Exception e){
                this.errorLogWriter(e, "index", this.screenID);
                throw e;
            }
        }else{
            return this.topTransition;
        }

    }
    /**
     * 入力内容登録処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = true, input="/csas/csas202.jsp")
    public String updateRegInfo () throws Exception{
        String transition = "/csas/csas202.jsp"; // 遷移先
        String result = "";

        // ログイン済判定する
        if(this.judgeLogin(csas202Form.getSessionId())){
            try{
                // リファラ判定
                if(this.judgeRefferer()){
                    csas202Service = new Csas202Service();
                    csas202Service.setFirstName(this.csas202Form.firstName);
                    csas202Service.setLastName(this.csas202Form.lastName);
                    csas202Service.setCompany(this.csas202Form.company);
                    csas202Service.setDepartment(this.csas202Form.department);
                    csas202Service.setPhone(this.csas202Form.phone);
                    csas202Service.setRegion(this.csas202Form.region);
                    csas202Service.setCountry(this.csas202Form.country);
                    csas202Service.setCity(this.csas202Form.city);
                    csas202Service.setUserID(((LoginDto)RequestUtil.getRequest().getSession().getAttribute("login_dto")).getUserID());

                    // データ更新
                    csas202Service.userUpgInfo();
                    transition = "/csas/csas203.jsp";
                    result = ConstantValue.success;

                    if(ConstantValue.success.equals(result)){
                        // 操作ログ記述
                        ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                        ArrayList<String> parameterNames = new ArrayList<String>();
                        ArrayList<String> values = new ArrayList<String>();
                        parameterNames.add(MessageResourcesUtil.getMessage("first_name"));
                        values.add(this.csas202Form.firstName);
                        parameterNames.add(MessageResourcesUtil.getMessage("last_name"));
                        values.add(this.csas202Form.lastName);
                        parameterNames.add(MessageResourcesUtil.getMessage("company"));
                        values.add(this.csas202Form.company);
                        parameterNames.add(MessageResourcesUtil.getMessage("department"));
                        values.add(this.csas202Form.department);
                        parameterNames.add(MessageResourcesUtil.getMessage("phone"));
                        values.add(this.csas202Form.phone);
                        parameterNames.add(MessageResourcesUtil.getMessage("region"));
                        values.add(this.csas202Form.region);
                        parameterNames.add(MessageResourcesUtil.getMessage("country"));
                        values.add(this.csas202Form.country);
                        parameterNames.add(MessageResourcesUtil.getMessage("city"));
                        values.add(this.csas202Form.city);
                        parameters.add(parameterNames);
                        parameters.add(values);
                        this.entryOperationHistory(this.screenID, "submit", parameters, result);
                    }

                    return transition;

                }else{
                    return this.topTransition;
                }
            }catch(Exception e){
                this.errorLogWriter(e, "insertRegInfo", "CSAS202");
                throw e;
            }
        }else{
            return this.topTransition;
        }
    }
}